#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys;
sys.path.append("..")
sys.path.append("../../")
from lib.BwRobotLib import BwRobotLib
from RobotSpider.SpiderRobot import SpiderRobot
import keyboard
import time

robotlib = BwRobotLib()
spiderRobot = SpiderRobot(robotlib)

#连接机器人设备
#robotlib.connectRobot("10.10.100.254",8899)
robotlib.connectRobot("192.168.1.107",55000)

#加载配置（兼容VS项目启动方式及文件直接启动方式）
import os,inspect
fileName = inspect.getframeinfo(inspect.currentframe()).filename
folderPath     = os.path.dirname(os.path.abspath(fileName))
cfile=folderPath+"/config.txt"
robotlib.loadConfig(cfile)

#配置ID
spiderRobot.bindIds(robotlib.module_ids)

#-----------------------------------------------------------------------------
#仿生蜘蛛
#-----------------------------------------------------------------------------

#处理键盘
def handleControlKeys(key):
    isDown = key.event_type == 'down'
    if not isDown:
        return

    cmd = key.name
    if(cmd == '1'):
        spiderRobot.goForward()        
    if(cmd == '2'):
        spiderRobot.goBack()
    if(cmd == '3'):
        spiderRobot.goForward_down()
    if(cmd == '4'):
        spiderRobot.goBack_down()
    if(cmd == '5'):
        spiderRobot.turnLeft()
    if(cmd == '6'):
        spiderRobot.turnRight()
    if(cmd == '7'):
        spiderRobot.turnLeft_down()
    if(cmd == '8'):
        spiderRobot.turnRight_down()
    if(cmd == '9'):
        spiderRobot.doUpAndDown()
    if(cmd == 'a'):
        spiderRobot.sayHello()
    if(cmd == 'b'):
        spiderRobot.doSport()
    if(cmd == 'c'):
        spiderRobot.doSport_oneHand()
    if(cmd == 'd'):
        spiderRobot.dance()
    if(cmd == 'e'):
        spiderRobot.doHangTail()
    if(cmd == 'f'):
        spiderRobot.doBattle()
    if(cmd == 'g'):
        spiderRobot.doMoveBody()
    pass


#控制提示
def controlTips():
    print("键盘控制命令 :")
    print("1 : 前进")
    print("2 : 后退")
    print("3 : 匍匐前进")
    print("4 : 匍匐后退")
    print("5 : 左转")
    print("6 : 右转")
    print("7 : 匍匐左转")
    print("8 : 匍匐右转")
    print("9 : 蹲下起立")
    print("a : 打招呼")
    print("b : 俯卧撑")
    print("c : 单手俯卧撑")
    print("d : 跳舞")
    print("e : 摇尾巴")
    print("f : 战斗防御")
    print("g : 扭动身体防御 ")
    print("-----------------------")
    print("0 :Exit")
    keyboard.hook(handleControlKeys)
    #参考Python 键盘/鼠标监听及控制：https://www.jianshu.com/p/8e508c6a05ce
    #helper.Wait(True)
    keyboard.wait('0')
    keyboard.unhook_all()

    pass

controlTips()